/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.coupon.entity.FxWithdrawalPerson;
import com.ykht.proj.coupon.service.FxWithdrawalPersonService;
import com.ykht.proj.coupon.vo.FxWithdrawalPersonVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.RichTextString;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * c端提现记录表
 *
 * @author proj code generator
 * @date 2021-08-13 16:55:14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxwithdrawalperson" )
@Api(value = "fxwithdrawalperson", tags = "c端提现记录表管理")
@Slf4j
public class FxWithdrawalPersonController {

	private final FxWithdrawalPersonService fxWithdrawalPersonService;

	/**
	 * 分页查询 c端提现记录表
	 *
	 * @param page                 分页对象
	 * @param fxWithdrawalPersonVo c端提现记录表
	 * @return
	 */
	@ApiOperation(value = "分页查询 c端提现记录表", notes = "分页查询 c端提现记录表")
	@GetMapping("/page")
	public R<IPage<FxWithdrawalPersonVo>> getFxWithdrawalPersonPage(Page page, FxWithdrawalPersonVo fxWithdrawalPersonVo) {
		log.info("->分页查询 c端提现记录表 :{}", page, fxWithdrawalPersonVo);
		IPage<FxWithdrawalPersonVo> iPage = fxWithdrawalPersonService.getFxWithdrawalPersonPage(page, fxWithdrawalPersonVo);
		return R.ok(iPage);
	}


	/**
	 * 通过id查询c端提现记录表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxwithdrawalperson_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxWithdrawalPersonService.getById(id));
	}

	/**
	 * 新增c端提现记录表
	 *
	 * @param fxWithdrawalPerson c端提现记录表
	 * @return R
	 */
	@ApiOperation(value = "新增c端提现记录表", notes = "新增c端提现记录表")
	@SysLog("新增c端提现记录表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxwithdrawalperson_add')")
	public R save(@RequestBody FxWithdrawalPerson fxWithdrawalPerson) {
		return R.ok(fxWithdrawalPersonService.save(fxWithdrawalPerson));
	}

	/**
	 * 修改c端提现记录表
	 *
	 * @param fxWithdrawalPerson c端提现记录表
	 * @return R
	 */
	@ApiOperation(value = "修改c端提现记录表", notes = "修改c端提现记录表")
	@SysLog("修改c端提现记录表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxwithdrawalperson_edit')")
	public R updateById(@RequestBody FxWithdrawalPerson fxWithdrawalPerson) {
		return R.ok(fxWithdrawalPersonService.updateById(fxWithdrawalPerson));


	}

	/**
	 * 通过id删除c端提现记录表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除c端提现记录表", notes = "通过id删除c端提现记录表")
	@SysLog("通过id删除c端提现记录表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxwithdrawalperson_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(fxWithdrawalPersonService.removeById(id));
	}




	@ApiOperation(value = "导出客户提现流水")
	@PostMapping("ExportExcel")
	@Inner(false)
//	@PreAuthorize("@pms.hasPermission('coupon_fxwithdrawalperson_Export')")
	public void ExportExcel(HttpServletResponse response) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("客户体现的流水表");
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell=row.createCell((short)1);
		cell.setCellValue("订单号");
		cell=row.createCell((short)2);
		cell.setCellValue("提现时间");
		cell=row.createCell((short)3);
		cell.setCellValue("用户名");
		cell=row.createCell((short)4);
		cell.setCellValue("手机");
		cell=row.createCell((short)5);
		cell.setCellValue("提现金额");
		cell=row.createCell((short)6);
		cell.setCellValue("剩余金额");
		cell=row.createCell((short)7);
		cell.setCellValue("提现方式");
		cell=row.createCell((short)8);
		cell.setCellValue("微信交易");
		List<FxWithdrawalPersonVo> list=fxWithdrawalPersonService.getExport();
		for (int i = 0; i <list.size() ; i++) {
			HSSFRow row1 = sheet.createRow((int) i + 1);
			FxWithdrawalPersonVo fx=list.get(i);
			row1.createCell((short)0).setCellValue(fx.getId());
			row1.createCell((short)1).setCellValue(fx.getOrderNumber());
			String currentTime = fx.getSuccessTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
			row1.createCell((short)2).setCellValue(currentTime);
			row1.createCell((short)3).setCellValue(fx.getPersonName());
			row1.createCell((short)4).setCellValue(fx.getPersonPhone());
			row1.createCell((short)5).setCellValue(fx.getMoney().toString());
			row1.createCell((short)6).setCellValue( fx.getBalance().toString());
			row1.createCell((short)7).setCellValue(fx.getWithdrawalType());
			row1.createCell((short)8).setCellValue(fx.getWxOrder());
		}
		try {
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("liushui.xls", "UTF-8"));
			response.setHeader("Connection", "close");
			response.setHeader("Content-Type", "application/octet-stream");
			wb.write(response.getOutputStream());
			wb.close();
		} catch (Exception e) {
			System.out.println("导出失败");
			e.printStackTrace();
		}

	}
}